﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MobileAPI.Web.Data;
using MobileAPI.BLL;
using MobileAPI.Entity.Model;
using MobileAPI.Common;

namespace MobileAPI.Web.APIs
{
    [MobileAPI(Code = "MC0003", Name = "搜索报修列表")]
    public class SearchRepairsList : IProcessMessage
    {
        private BLLRepairsInfo bll;
        public BLLRepairsInfo Bll
        {
            get
            {
                if (bll == null)
                    bll = BLLFactory.getInstance().CreateRepairsInfoBLL();
                return bll;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {    
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                string UserName = reqMsg.Body.GetFieldValue("UserName");
                string CustomerName = reqMsg.Body.GetFieldValue("CustomerName");
                string RepairsId = reqMsg.Body.GetFieldValue("RepairsId");
                string StartDate = reqMsg.Body.GetFieldValue("StartDate");
                string EndDate = reqMsg.Body.GetFieldValue("EndDate");
                string Progress = reqMsg.Body.GetFieldValue("Progress");

                if (string.IsNullOrEmpty(UserName)||string.IsNullOrEmpty(StartDate) || string.IsNullOrEmpty(EndDate))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                List<ERepairsInfo> list = (List<ERepairsInfo>)Bll.SearchTodayRepairsList(UserName, CustomerName, RepairsId, StartDate, EndDate, Progress);
                if (list == null||list.Count==0)
                {
                    rspMsg.Head.RspCode  = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;

                    return rspMsg;
                }
                else
                {
                    // 返回内容（具体请参见文档）
                    for (int i = 0; i < list.Count; i++)
                    {
                        var fl = rspMsg.Body.AddFieldList("RepairsInfo");
                        fl.AddField("RepairsId", list[i].RepairsId.TString());
                        fl.AddField("CustomerName", list[i].CustomerName);
                        fl.AddField("Diagnosis", list[i].Diagnosis);
                        fl.AddField("Instrument", list[i].Instrument);
                        fl.AddField("Progress", list[i].Progress);
                    }

                    rspMsg.Head.RspCode = MessageCode.Success.Code;
                    rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                    return rspMsg;
                }
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("Login-" + e);
                throw e;
            }

        }
        #endregion
    }
}